% This is the style and customized commands and macros for the dissertation of Xiaodong Qi. Most of the code in this file were accumulated overtime in writing LaTeX documentations. The rest are borrowed from the UNM dissertation template, as well as the dissertation styles of Ben Baragiola and Zhang Jiang under their permission of use and redistribution for future publications. 
% By Xiaodong Qi (qxd@unm.edu), 2018-1-14.

%#####################################################################################
%## general 
\RequirePackage{pdfpages} %This is used to include the approval page.
\pdfminorversion=7 %This prevents the PDFLaTeX Warning: Found pdf version 1.7, allowed maximum 1.6.
\RequirePackage{microtype}% reduce the number of badboxes?
\RequirePackage{graphicx}
\RequirePackage[margin=10pt]{caption} 
\RequirePackage{catchfilebetweentags} % This is a very handy package that allows one 
% to capture a part of a file surrounded by the tags %<*tag> and %</tag>. 
%\RequirePackage{epstopdf}
%\RequirePackage{geometry} %\geometry{left=1.5in, bottom=1in, top=1in, right=1in}
%\RequirePackage{epsfig}
\RequirePackage{standalone}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% ``catchfilebetweentags'' is a very handy package that allows one to capture a part of a file surrounded by the tags %<*tag> and %</tag>. However, the authors decided to issue \endlinechar=-1, which causes the linebreaks vanish. The following codes is a workaround from tex.stackexchange article ``\input only part of a file using catchfilebetweentags package.''
\makeatletter
\def\CatchFBT@Fin@l#1[#2]{%
   \begingroup
      %\endlinechar\m@ne % <- this is the guilty party
      \makeatletter #2%
      \scantokens\expandafter{%
         \expandafter\CatchFBT@tok\expandafter{\the\CatchFBT@tok}}%
      \CatchFBT@IsAToken{#1}
         {\global#1\expandafter{\the\CatchFBT@tok}}
         {\xdef#1{\the\CatchFBT@tok}}%
      \ifx\CatchFBT@tok#1\else\global\CatchFBT@tok{}\fi
   \endgroup
}% \CatchFBT@Final
\makeatother

\newcommand\hmmax{0} % This and the following line is to solve the error on "Too many math alphabets in math normal". See https://tex.stackexchange.com/questions/3676/too-many-math-alphabets-error
\newcommand\bmmax{0}
\usepackage{bm}
\usepackage{bbm}
\usepackage{listings}
% % \textwidth 16cm \textheight 23.5cm
% \renewcommand{\baselinestretch}{1.2}
\usepackage{graphicx}
\usepackage{graphics}
\usepackage{epsfig}
\usepackage{color}
\usepackage{multirow}
\usepackage{fancyhdr}
\usepackage{calc}
\usepackage[numbers]{natbib}
\usepackage{bibentry}
% underline tool
\usepackage[normalem]{ulem}

%\uline{foo}	Underlines foo
%\uuline{foo}	Double underlines foo
%\uwave{foo}	Underlines foo with a wavy line
%\sout{foo}	Strikesout foo
%\xout{foo}	Crosses out foo with ¡®/6¤7¡¯
% triple lines in colors
\makeatletter
\newcommand\uuuline{\bgroup\markoverwith%
   {%
     \textcolor{red}{\rule[-0.5ex]{2pt}{0.4pt}}%
     \llap{\textcolor{blue}{\rule[-0.7ex]{2pt}{0.4pt}}}%
     \llap{\textcolor{green}{\rule[-0.9ex]{2pt}{0.4pt}}}%
   }%
   \ULon}
\makeatother


\usepackage{amsmath,soul} % underline with a number
\allowdisplaybreaks % To avoid long equations in a line not to break into two or more lines.
% usage example: $\underset{4}{\text{\ul{This is short text}}}$
% another package to use, but did not work for me.
% From: http://tex.stackexchange.com/questions/45341/labeling-underlined-text-over-multiple-lines
%\usepackage{soulpos}
%\ulposdef{\ulnumaux}{%
%   $\underset{\saveulnum}{\rule[-.7ex]{\ulwidth}{.4pt}}$}
%
%\newcommand{\ulnum}[2]{%
%  \def\saveulnum{#1}%
%  \ulnumaux{#2}}

% todo list and commands
\usepackage{todonotes}
%% to avoid the conflict with amths package % not working
%\makeatletter
%\providecommand\@dotsep{5}
%\makeatother
%\listoftodos\relax
\usepackage{imakeidx} % Instead of using makeidx, this imakeidx package allows hyperlinks on page numbers for the indexed items. The hyperlink package is required to put at the end of loading packages.
%\makeindex[ % This is for index with special characters.
%  intoc,
%  program=xindy,
%  options=-C utf8 
%]

\allowdisplaybreaks
% for eps transfering to pdf.
\usepackage[update,prepend]{epstopdf}
\usepackage{ifpdf}

\ifpdf
   \usepackage{graphicx}
   \usepackage{epstopdf}
   \epstopdfsetup{suffix=}
   \DeclareGraphicsRule{.eps}{pdf}{.pdf}{`epstopdf #1}
   \pdfcompresslevel=9
\else
   \usepackage{graphicx}
\fi
% subfig
\usepackage{mwe}
\usepackage{subfig}
% to fix a figure's position using [H] option of thec figure.
\usepackage{float}

% This part of code fix the placement of figures to corresponding subsections level. See https://tex.stackexchange.com/questions/118662/use-placeins-for-subsections
\usepackage{placeins}
\makeatletter
\AtBeginDocument{%
  \expandafter\renewcommand\expandafter\subsection\expandafter
    {\expandafter\@fb@secFB\subsection}%
  \newcommand\@fb@secFB{\FloatBarrier
    \gdef\@fb@afterHHook{\@fb@topbarrier \gdef\@fb@afterHHook{}}}%
  \g@addto@macro\@afterheading{\@fb@afterHHook}%
  \gdef\@fb@afterHHook{}%
}
\makeatother


% self-defined short-cuts and commands

% packages we need for judging the operating system
% compile your tex file with option -shell-escape is required: 
% e.g. xelatex -shell-escape file.tex
\usepackage{pdftexcmds}
\usepackage{catchfile}
\usepackage{ifluatex}
\usepackage{ifplatform}

% To use subfigures. From http://tex.stackexchange.com/questions/75014/is-it-possible-to-make-a-reference-to-a-subfigure-to-appear-figure-2a-with-cle
\captionsetup[subfigure]{subrefformat=simple,labelformat=simple,listofformat=subsimple}
\renewcommand\thesubfigure{(\alph{subfigure})}

%\includeonly{VectorTensor}
% for table captions.
\usepackage{tabularx,ragged2e,booktabs,caption}

% packages for drawing. Some colors are defined for a nanofiber diagram drawing.
\usepackage{tikz,pgfplots}
% recommended as of Pgfplots 1.3: See also https://tex.stackexchange.com/questions/31276/number-format-in-pgfplots-axis
%\pgfplotsset{compat=newest}
\pgfplotsset{ % Here we specify options for all figures in the document
  compat=1.8, % Which version of pgfplots do we want to use?
  legend style =
  {font=\small\sffamily},
  label style = {font=\small\sffamily},
  xticklabel style={/pgf/number format/.cd, 1000 sep={}}
  }
\usepackage{tikzscale} % To use TikZ pictures in the includegraphics environment and rescale the input picture properly. See https://tex.stackexchange.com/questions/36297/pgfplots-how-can-i-scale-to-text-width
\usepackage{xparse}
\usetikzlibrary{calc,decorations.markings,arrows,matrix,backgrounds,babel} % The babel package could potentially fix the catcode problem with $$. See https://tex.stackexchange.com/questions/31860/conflict-onlyamsmath-and-tikz
\pgfdeclarelayer{myback}
\pgfsetlayers{myback,background,main}
\tikzset{mycolorEq/.style = {line width=1bp,color=#1}}%
\tikzset{myfillcolor/.style = {draw,fill=#1}}%

\NewDocumentCommand{\highlight}{O{blue!40} m m}{%
\draw[mycolorEq=#1,rounded corners,inner sep=0pt,opacity=0.8] (#2.north west)rectangle (#3.south east);
}

\NewDocumentCommand{\fhighlight}{O{blue!40} m m}{%
\draw[myfillcolor=#1,rounded corners,inner sep=0pt,opacity=0.8] (#2.north west)rectangle (#3.south east);
}

%##### Customization by Zhang Jiang based on the UNM dissertation template ##########
%## symbols and notations
%\RequirePackage{dtklogos}% provide logos such as \AMS
\RequirePackage{easybmat}% for block matrices
\RequirePackage{amssymb} % to use \lesssim and other math symbols
\RequirePackage{amsmath}
%\RequirePackage{amstext}
\let\unmdegree\degree   
\let\degree\undefined
\RequirePackage{SIunits} %Due to conflicts of the SIunits package and other three packages (unmthesis, amsfonts, and MnSymbol), I have redefined some commands that are identically defined more than one times.
\let\SIunitsdegree\degree
\let\degree\unmdegree
\let\SIunitsquare=\square
\RequirePackage{amsfonts}
\let\amsfontssquare=\square
%\RequirePackage{MnSymbol} %Individual symbols from this package is defined below.
%\let\MnSymbolsquare=\square
\let\square=\SIunitssquare
\RequirePackage{mathrsfs}% for math font \mathscr
\RequirePackage{accents} %This package allows a thicker version of over dot for time derivatives.
\RequirePackage{subdepth}%Subscript vertical position dependent on presence of superscript
\RequirePackage{upgreek}
\RequirePackage{bbold}% for blackboard bold Arabic numerals
\RequirePackage{dsfont} % for better double stroke fonts and the 'identity operator'
\RequirePackage{sansmath}% for slanted math sans serif fonts, which is used in the symplectic stuff
\RequirePackage{latexsym}
%\RequirePackage[matrix,frame,arrow]{xy}
%\RequirePackage{array}
%\RequirePackage{multirow}
%\RequirePackage{mathtools}
%\RequirePackage{tabularx}
%\RequirePackage{bbm}
%\AtBeginDocument{\catcode`\$=3} % Disable the $$ check protection feature of ams packages.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{etoolbox} % These three lines are used to fix the potential catcode problem with $$ in the TikZ environment. See https://tex.stackexchange.com/questions/31860/conflict-onlyamsmath-and-tikz
\preto\tikzpicture{\catcode`$=3 }
\preto\tikz{\catcode`$=3 }

%\usepackage[colorlinks]{hyperref}
%########### Customization by Zhang Jiang based on the UNM template ############
%## hyperref 
\RequirePackage{xcolor}
\definecolor{dark-red}{rgb}{0.4,0.15,0.15}
\definecolor{dark-blue}{rgb}{0.15,0.15,0.4}
\definecolor{medium-blue}{rgb}{0,0,0.5}
\definecolor{OliveGreen}{rgb}{0,0.6,0}
\definecolor{RedViolet}{rgb}{0.78, 0.08, 0.52}
\definecolor{safetyorange}{rgb}{1.0, 0.4, 0.0}
\definecolor{amber}{rgb}{1.0, 0.49, 0.0}
\definecolor{brilliantrose}{rgb}{1.0, 0.33, 0.64}
\RequirePackage[pagebackref]{hyperref}
\RequirePackage[all]{hypcap}%fix problems with figures and tables of the 'hyperref' package
\RequirePackage{cleveref}% Package hperref works wrongly within the 'subequations' environment without this.
\hypersetup{
    linktocpage, % linktoc=all, % Enable this option if you prefer colored Table of Content items.
    colorlinks, linkcolor={dark-red},
    citecolor={dark-blue}, urlcolor={medium-blue}
} %These setups replace the ugly and distracting red and green boxes of the 'hyperref' package with colored texts. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%########################## Shortcuts ######################################
%## bibtex
\newcommand{\intro}{ch.1}
\newcommand{\waveguide}{ch.2}
\newcommand{\id}[1]{\ {\color{purple}Ivan: #1}\ }
\newcommand{\qxd}[1]{\ {\color{blue}Qi: #1}\ }
\newcommand{\teves}[1]{\ {\color{green!55!blue}Teves: #1}\ }
\newcommand{\nd}[1]{\ {\color{magenta}ND: #1}\ }
\newcommand{\gopi}[1]{\ {\color{RedViolet}Gopi: #1}\ }
\newcommand{\sr}[1]{\ {\color{red!70!blue}SR: #1}\ }
\newcommand{\pablo}[1]{\ {\color{red!40!green}Pablo: #1}\ }
\newcommand{\ra}[1]{\ {\color{OliveGreen}RA: #1}\ }
\newcommand{\rie}[1]{\ {\color{safetyorange}Rie: #1}\ }
\newcommand{\af}[1]{\ {\color{amber}AF: #1}\ }
\newcommand{\satomi}[1]{\ {\color{brilliantrose}Satomi: #1}\ }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%######################### Some overall settings ############################
%## miscellaneous  
\newcommand{\req}[1]{Eq.~(\ref{#1})} %refer to an equation
%\newcommand{\peq}[1]{\left\{\begin{aligned}#1 \end{aligned}\right.}%equations in parentheses
\newcommand{\lb}[1]{\label{#1}}% These two commands \lb abd \rf will be redefined before each chapter so that I don't have to worry about conflicts of labels cross over the chapters.
\newcommand{\rf}[1]{\ref{#1}}% When cross chapter references are needed, use \ref.
\graphicspath{{../media/Figs/}} %include the image searching directory
\raggedbottom %This command tells PDFLaTeX to put the extra spaces at the bottom of a page instead of stretching the texts and producing a 'bad box' warning.
\hfuzz=5pt % Warning me only if the line is overstretched by this amount. Set is large will surpress the black boxes. See https://tex.stackexchange.com/questions/50830/do-i-have-to-care-about-bad-boxes/50850#50850
%\setcounter{tocdepth}{3} %specify what depth to take the Table of Contents to
%\bibliographystyle{../styles/abbrv-alpha-letters} % this bib style is based on 'alpha-letters', and the first names of authors are abbreviated.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%######################### Macros by Zhang Jiang #######################
\providecommand{\openone}{\leavevmode\hbox{\small1\kern-3.8pt\normalsize1}}% APS openone
\newcommand{\identity}{\mbox{\textbb{1}}}% For unknown reasons, 'bbold' and 'dsfonts' conflict with 'sansmath' in the math mode, which I have to use. This is a work around by using the text version of 'bbold'.
\newcommand{\nullmatrix}{\mbox{\textbb{0}}}% same as above
\newcommand{\smallnullmatrix}{\mbox{\footnotesize \textbb{0}}}% same as above
\newcommand{\overbar}[1]{\mkern 1.5mu\overline{\mkern-1.5mu#1\mkern-1.5mu}\mkern 1.5mu} %long over bar (compared to \bar)
\newcommand{\thickbar}[1]{\accentset{\rule{.45em}{.6pt}}{#1}}
\newcommand{\longthickbar}[1]{\accentset{\rule{.65em}{.7pt}}{#1}}
%## all kinds of \psi and \phi
\newcommand{\psibf}{\boldsymbol{\psi}}
\newcommand{\psibftl}{\widetilde{\boldsymbol{\psi}}}
\newcommand{\psibfdt}{\boldsymbol{\dt\psi}}
\newcommand{\phibf}{\boldsymbol{\phi}}
\newcommand{\phibftl}{\widetilde{\boldsymbol{\phi}}}
\newcommand{\phibfdt}{\boldsymbol{\dt\phi}}
\newcommand{\PsiErr}{\varPsi_\mathrm{err}}
%## variables that I change every minute
\newcommand{\mathsp}[1]{\mbox{\sansmath $#1$}}
\newcommand{\mathspst}[1]{\mbox{\sansmath $\scriptstyle  #1$}}
\newcommand{\transp}{{\scriptstyle{T}}}
\renewcommand{\a}{a}
\renewcommand{\b}{b}
\newcommand{\abf}{\mathbf{a}}
\newcommand{\bbf}{\mathbf{b}}
\newcommand{\hatx}{x}
\newcommand{\hatp}{p}
\newcommand{\rank}{\nu}%{{\mbox{\hspace{-0.4pt}\small\it s}}}
\newcommand{\Rank}{\nu}
\newcommand{\nrank}{n}
\newcommand{\NFB}{\mathsf{N}} %normalization factor of TPS for bosons
\newcommand{\NFF}[1]{\mathsf{N}_{\smash{\vec{\gamma}},{#1}}}%normalization factor of TPS for fermions
\newcommand{\BesselI}{I}
\newcommand{\idu}{u}
\newcommand{\idv}{v}
\newcommand{\idg}{g}
\newcommand{\idG}{G}
\newcommand{\idh}{\delta}
\newcommand{\ids}{s}
\newcommand{\idT}{T}
\newcommand{\ssB}{{\scriptscriptstyle B}}
\newcommand{\ssC}{{\scriptscriptstyle C}}
\newcommand{\ssK}{{\scriptscriptstyle K}}
\newcommand{\ssL}{{\scriptscriptstyle L}}
\newcommand{\ssR}{{\scriptscriptstyle R}}
\newcommand{\ssO}{{\scriptscriptstyle O}}
\newcommand{\ssV}{{\scriptscriptstyle V}}
\newcommand{\ssU}{{\scriptscriptstyle U}}
\newcommand{\ssW}{{\scriptscriptstyle W}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Commands from Ben's nanofiberInterface note. 
%\newcommand{\melement}[3]{\langle #1 \lvert #2 \rvert #3 \rangle}

%\newcommand{\Ip}[2]{\left\langle {#1},{#2} \right\rangle}
%\newcommand{\modsq}[1]{\lvert #1 \rvert^2}
%\newcommand{\normsq}[1]{\lVert #1 \rVert^2}
%\newcommand{\expect}[1]{\langle #1 \rangle}
%\newcommand{\grad}{\nabla}
%\newcommand{\partialD}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\Eq}[1]{Eq. [\ref{Eq::#1}]}

\newcommand{\xit}{\xi(t)}
\newcommand{\xits}{\xi^*(t)}
\newcommand\foreign[1]{{\it #1\spacefactor=1000}}
\newcommand\eg{\foreign{e.g.}}
\newcommand\ie{\foreign{i.e.}}
\newcommand\ibid{\foreign{ibid}}
\newcommand\Denv{{D_{\mbox{\scriptsize env}}}}
\newcommand\Dcl {{D_{\mbox{\scriptsize cl }}}}
\newcommand{\sq}[1]{\left[ {#1} \right]}
%\newcommand{\tr}[1]{{\textrm {Tr}}\sq{#1}}
%\newcommand{\smallfrac}[2]{\mbox{$\frac{#1}{#2}$}}
%\newcommand{\bra}[1]{\langle{#1}|}
%\newcommand{\ket}[1]{|{#1}\rangle}
%\newcommand{\ip}[2]{\langle{#1}|{#2}\rangle}
%\newcommand{\op}[2]{\ket{#1}\bra{#2}}
%\newcommand{\enavg}[1]{\mathrm{E}\sq{#1}}
%\newcommand{\gravg}[1]{\mathbb{E}\sq{#1}}
%\newcommand{\expt}[1]{\langle{#1}\rangle}
%\newcommand{\dg}{^\dagger}
\newcommand{\D}[1]{{\cal D}\sq{#1}}
\newcommand{\J}[1]{{\cal J}\sq{#1}}
\newcommand{\Hc}[1]{{\cal H}\sq{#1}}
\newcommand{\Hcun}[1]{\tilde{\cal H}\sq{#1}}
\newcommand{\Lc}[1]{{\cal L}_c\sq{#1}}
\newcommand{\Lcun}[1]{{\tilde{\cal L}}_c\sq{#1}}
\newcommand{\Luc}[1]{{\cal L}\sq{#1}}
%\newcommand{\emn}[1]{ \mathbbm{E}_{#1} }

\newcommand{\beq}{\begin{equation}}
\newcommand{\eeq}{\end{equation}}
\newcommand{\bqa}{\begin{eqnarray}}
\newcommand{\eqa}{\end{eqnarray}}
%\newcommand{\nn}{\nonumber}
\newcommand{\nl}[1]{\nn \\ && {#1}\,}
%\newcommand{\erf}[1]{Eq.~(\ref{#1})}
%\newcommand{\frf}[1]{Fig.~\ref{#1}}
%\newcommand{\srf}[1]{Sec.~\ref{#1}}
%\newcommand{\dg}{^\dagger}
\newcommand{\rt}[1]{\sqrt{#1}\,}
%\newcommand{\smallfrac}[2]{\mbox{$\frac{#1}{#2}$}}
%\newcommand{\half}{\smallfrac{1}{2}}
%\newcommand{\bra}[1]{\langle{#1}|}
%\newcommand{\ket}[1]{|{#1}\rangle}
%\newcommand{\ip}[2]{\langle{#1}|{#2}\rangle}
%\newcommand{\sch}{Schr\"odinger}
%\newcommand{\hei}{Heisenberg }
%\newcommand{\ein}{Einstein }
%\newcommand{\Tr}{\mbox{Tr}}
\newcommand{\red}{\color{red}}
\newcommand{\blue}{\color{blue}}
%\newcommand{\tensor}[1]{\boldsymbol{#1}}
\newcommand{\tensor}[1]{\overset{\leftrightarrow}{#1}} %{\stackrel{\leftrightarrow}{#1}}

%==== Ben's new macros ======

\newcommand{\fidx}{\eta}
\newcommand{\chibir}{\chi_{\rm bir}}

\newcommand{\gammap}{\tilde{\gamma}_\uparrow + \tilde{\gamma}_\downarrow}
\newcommand{\gammam}{\tilde{\gamma}_\uparrow - \tilde{\gamma}_\downarrow}

%==== effective areas ======
\newcommand{\Abiref}{A_{\rm bir}}
\newcommand{\Aeff}{A_{\rm eff}}

%==== colors for editing ======
\newcommand{\change}[1]{{\color{blue} #1}}
\newcommand{\comment}[1]{{\color{orange} #1}}
\newcommand{\error}[1]{{\color{red} #1}}


% Other self-definition for short hand

% Overline bar to be thicker.
\RequirePackage{accents}
%\renewcommand\thickbar[1]{\accentset{\rule{.4em}{.8pt}}{#1}}

% symbols and math operators
\DeclareMathOperator{\spn}{span}
\DeclareMathOperator{\tr}{tr}
\DeclareMathOperator{\sgn}{sgn}
\DeclareMathOperator{\diag}{diag}
\DeclareMathOperator{\re}{Re}
\DeclareMathOperator{\im}{Im}
\DeclareMathOperator{\poly}{Poly}
\DeclareMathOperator{\Corr}{Corr}
% definition of grammars % formats related
\definecolor{MyDarkGreen}{rgb}{0.0,0.4,0.0}
\newcommand{\greek}[1]{{\selectlanguage{greek}#1}} % will look for grmn font: tlmgr install cbfonts (65 MB)

% functions
\newcommand{\sn}{\mathrm{sn}}
\newcommand{\cn}{\mathrm{cn}}
\newcommand{\dn}{\mathrm{dn}}

% constants
\newcommand{\invtpi}{\frac{1}{2\pi}}

% vectors and tensors
\def\en{\mathbf{e}_n}
\def\eye{\tensor{\mathbbm{1}}}
\newcommand\lvec[1]{\accentset{\leftarrow}{#1}}
% math font
\newcommand{\bmc}[1]{\boldsymbol{\mathcal{#1}}}

% derivatives and integrals
% ordinary derivatives
\newcommand{\drv}{\mathrm{d}}
\newcommand{\dt}[1]{\frac{{\mathrm d} {#1}}{{\mathrm d}t}}
\newcommand{\dx}[1]{\frac{{\mathrm d} {#1}}{{\mathrm d}x}}
\newcommand{\dtau}{\frac{{\mathrm d} }{{\mathrm d}\tau}}
\newcommand{\dd}[2]{\frac{{\mathrm d} {#1}}{{\mathrm d} {#2}}}
\newcommand{\sdt}[1]{\frac{{\mathrm d}^2 {#1}}{{\mathrm d}t^2}}
\newcommand{\sdx}[1]{\frac{{\mathrm d}^2 {#1}}{{\mathrm d}x^2}}
\newcommand{\sdd}[2]{\frac{{\mathrm d}^2 {#1}}{{\mathrm d}{#2}^2}}
\newcommand{\ddn}[3]{\frac{{\mathrm d}^{#1} #2}{{\mathrm d} #3 ^{#1}}}

% partial derivatives
\newcommand{\pt}[1]{\frac{\partial {#1}}{\partial t}}
\newcommand{\px}[1]{\frac{\partial {#1}}{\partial x}}
\newcommand{\pp}[2]{\frac{\partial {#1}}{\partial {#2}}}
\newcommand{\spt}[1]{\frac{\partial^2 {#1}}{\partial t^2}}
\newcommand{\spx}[1]{\frac{\partial^2 {#1}}{\partial x^2}}
\newcommand{\spp}[2]{\frac{\partial^2 {#1}}{\partial {#2}^2}}
\newcommand{\ppn}[3]{\frac{\partial^{#1} #2}{\partial #3 ^{#1}}}
% integrals
\newcommand{\intl}[2]{\int_0^\infty\! #1 \mathrm{d}#2}
\newcommand{\intf}[2]{\int_{-\infty}^\infty\! #1 \mathrm{d}#2}


% quantum operators
\newcommand{\ssp}{\braket{\sigma^{+}(t)\sigma^{-}(t)}}
\newcommand{\aap}{\braket{a^{\dagger}(t)a(t)}}
\newcommand{\as}{\braket{a^{\dagger}(t)\sigma^{-}(t)}}
\newcommand{\sa}{\braket{a(t)\sigma^{+}(t)}}
\newcommand{\Hssp}{\braket{\sigma^{+}\sigma^{-}}}
\newcommand{\Haap}{\braket{a^{\dagger}a}}
\newcommand{\Has}{\braket{a^{\dagger}\sigma^{-}}}
\newcommand{\Hsa}{\braket{a\sigma^{+}}}
\newcommand{\adag}{a^{\dagger}}
\newcommand{\sigm}{\sigma^{-}}
\newcommand{\sigp}{\sigma^{+}}
\newcommand{\sigz}{\sigma^{z}}
\newcommand{\gp}{\gamma^{\prime}}
\newcommand{\oal}{\omega_a-\omega_0}
\newcommand{\ocl}{\omega_c-\omega_0}


% Green function related
\def\GFT{\tensor{\bf G}}
\def\unittensor{\tensor{\bf I}}
\def\IT{\overline{\bf I}}
\def\TT{\overline{\bf T}}
\def\MT{\overline{\bf M}}
\def\AT{\overline{\bf A}}
\def\BT{\overline{\bf B}}
\def\fT{\overline{\bf f}}
\def\LT{\overline{\bf L}}
\def\alphaT{\overline{\bf \alpha}}
\def\GFTr{\overline{\bf G}\left(\mathbf{r},\mathbf{r}'\right)}
\def\GFTrw{\overline{\bf G}\left(\mathbf{r},\mathbf{r}';\omega\right)}
\def\rarg{\left(\mathbf{r}\right)}
\def\rargw{\left(\mathbf{r};\omega\right)}
\def\rrarg{\left(\mathbf{r},\mathbf{r}'\right)}
\def\rrargw{\left(\mathbf{r},\mathbf{r}';\omega\right)}
\def\rk{\left(\mathbf{r}_k\right)}
\def\rn{\left(\mathbf{r}_n\right)}
\def\rnrn{\left(\mathbf{r}_n,\mathbf{r}_n\right)}
\def\rnrk{\left(\mathbf{r}_n,\mathbf{r}_k\right)}
\def\rkrk{\left(\mathbf{r}_k,\mathbf{r}_k\right)}
\def\rkrn{\left(\mathbf{r}_k,\mathbf{r}_n\right)}
\def\br{\mathbf{r}}
\def\Erw{\hat{\mathbf{E}}(\mathbf{r},\omega)}
\def\E0{\hat{\mathbf{E}}^{(0)}(\mathbf{r},\omega)}
\def\Arw{\hat{\mathbf{A}}(\mathbf{r},\omega)}
\def\Snw{\hat{\mathbf{S}}_n(\omega)}
\def\Unw{\mathbf{U}_n(\omega)}
\def\unw{U_n(\omega)}
\def\Alphanw{{\bm \alpha}_n(\omega)}
\def\Krrw{\mathbf{K}(\mathbf{r},\mathbf{r}',\omega)}
\def\Krrnw{\mathbf{K}(\mathbf{r},\mathbf{r}_n,\omega)}
\def\GTrrw{\mathbf{G}^T(\mathbf{r},\mathbf{r}',\omega)}
\def\Grrw{\mathbf{G}(\mathbf{r},\mathbf{r}',\omega)}
\def\Gn{\mathbf{G}^n}
\def\Gm1{\mathbf{G}^{n-1}}
\def\GN{\mathbf{G}^N}
\def\G0{\mathbf{G}^0}
\def\G1{\mathbf{G}^1}
\def\Gi{\mathbf{G}^i}
\def\flamr{\mathbf{f}_\lambda(\mathbf{r})}
\def\rrn{\mathbf{r},\mathbf{r}_n}
\def\rnrn{\mathbf{r}_n,\mathbf{r}_n}
\def\rr{\mathbf{r},\mathbf{r}'}


% braket.sty          Macros for Dirac bra-ket <|> notation and sets {|}
%
\def\bra#1{\langle{#1}\rvert}%{\mathinner{\langle{#1}\rvert}}
\def\ket#1{\lvert{#1}\rangle}%{\mathinner{\lvert{#1}\rangle}}
\def\braket#1{\mathinner{\langle{#1}\rangle}}
\def\Bra#1{\left<#1\right|}
\def\Ket#1{\left|#1\right>}
{\catcode`\|=\active
  \gdef\Braket#1{\left<\mathcode`\|"8000\let|\BraVert {#1}\right>}}
\def\BraVert{\egroup\,\mid@vertical\,\bgroup}
{\catcode`\|=\active
  \gdef\set#1{\mathinner{\lbrace\,{\mathcode`\|"8000\let|\midvert #1}\,\rbrace}}
  \gdef\Set#1{\left\{\:{\mathcode`\|"8000\let|\SetVert #1}\:\right\}}}
\def\midvert{\egroup\mid\bgroup}
\def\SetVert{\egroup\;\mid@vertical\;\bgroup}
% Some stuff deleted
% Macros for Dirac bra-ket <|> notation
%\def\bra#1{\mathinner{\langle{#1}|}}
%\def\ket#1{\mathinner{|{#1}\rangle}}
\def\Braket#1#2{\mathinner{\langle{#1}\! \mid\! {#2} \rangle}}
\def\ketbra#1{\ket{#1}\!\!\bra{#1}}
\newcommand{\Ketbra}[2]{\ket{#1}\!\!\bra{#2}}
\def\sandwich#1#2{\bra{#1}\! #2 \! \ket{#1}}
\def\Sandwich#1#2#3{\bra{#1}\! #2\! \ket{#3}}
%
% END  braket.sty     Macros for Dirac bra-ket <|> notation and sets {|}


%% Ben's shortcuts.
% Equation, citation, and labeling macros
%========================================================================================
\newcommand{\erf}[1]{Eq.~(\ref{#1})}
\newcommand{\frf}[1]{Fig.~\ref{#1}}
\newcommand{\srf}[1]{Sec.~\ref{#1}}
\newcommand{\nn}{\nonumber}
\newcommand{\mbf}[1]{\mathbf{#1}}
%========================================================================================
% General quantum mechanics macros
%========================================================================================
\newcommand{\ip}[2]{\langle{#1}|{#2}\rangle}
\newcommand{\op}[2]{\ket{#1}\bra{#2}}
\newcommand{\enavg}[1]{\mathrm{E}\sq{#1}}
\newcommand{\gravg}[1]{\mathbb{E}\sq{#1}}
\newcommand{\expt}[1]{\langle{#1}\rangle}
\newcommand{\dg}{^\dagger}
\newcommand{\smallfrac}[2]{\mbox{$\frac{#1}{#2}$}}
\newcommand{\emn}[1]{ \mathbbm{E}_{#1} }
\newcommand{\Tr}{\mbox{Tr}}
%\newcommand{\tensor}[1]{\boldsymbol{#1}}%{\overset{\leftrightarrow}{#1}}
%========================================================================================
% Famous physicists
%========================================================================================
\usepackage{xspace}
\newcommand{\sch}{Schr\"odinger\xspace}
\newcommand{\hei}{Heisenberg\xspace}
\newcommand{\ein}{Einstein\xspace}
\newcommand{\str}{Stratonovich\xspace}
%\newcommand{\Poincare}{Poincar\'e\index{Poincar\'e}\xspace}
\newcommand{\Poincare}{Poincar\'e\index{Poincar\'e sphere}\xspace}
%========================================================================================
% Dirac notation and commutators
%========================================================================================
\newcommand{\norm}[1]{\lvert #1 \rvert}
\newcommand{\opnorm}[1]{\lVert #1 \rVert}
%\newcommand{\ket}[1]{\lvert #1 \rangle}
%\newcommand{\bra}[1]{\langle #1 \rvert}
\newcommand{\bravket}[2]{\langle\, #1\,\vert\, #2 \,\rangle}
\newcommand{\av}[1]{\left\langle #1 \right\rangle}
\newcommand{\proj}[1]{\ket{#1}\!\bra{#1}}
\newcommand{\commut}[2]{[\ssp #1,\,#2\ssp]}
\newcommand{\anticommut}[2]{\{\ssp #1,\,#2\ssp\}}
% big Dirac notation and commutators
\newcommand{\bnorm}[1]{\big\lvert #1 \big\rvert}
\newcommand{\bopnorm}[1]{\big\lVert #1 \big\rVert}
\newcommand{\bket}[1]{\big\lvert\, #1\, \big\rangle}
\newcommand{\bbra}[1]{\big\langle\, #1\, \big\rvert}
\newcommand{\bbraket}[2]{\big\langle\, #1\,\big\vert\, #2 \,\big\rangle}
\newcommand{\bav}[1]{\bigl\langle #1 \bigr\rangle}
\newcommand{\bproj}[1]{\bket{#1}\!\bbra{#1}}
\newcommand{\bcommut}[2]{\big[\ssp #1,\,#2\ssp\big]}
\newcommand{\banticommut}[2]{\big\{\ssp #1,\,#2\ssp\big\}}
% Big Dirac notation and commutators
\newcommand{\Bnorm}[1]{\Big\lvert #1 \Big\rvert}
\newcommand{\Bopnorm}[1]{\Big\lVert #1 \Big\rVert}
\newcommand{\Bket}[1]{\Big\lvert\, #1\, \Big\rangle}
\newcommand{\Bbra}[1]{\Big\langle\, #1\, \Big\rvert}
\newcommand{\Bbraket}[2]{\Big\langle\, #1\,\Big\vert\, #2 \,\Big\rangle}
\newcommand{\Bav}[1]{\Bigl\langle #1 \Bigr\rangle}
\newcommand{\Bproj}[1]{\Bket{#1}\!\Bbra{#1}}
\newcommand{\Bcommut}[2]{\Big[\ssp #1,\,#2\ssp\Big]}
\newcommand{\Banticommut}[2]{\Big\{\ssp #1,\,#2\ssp\Big\}}
%========================================================================================
% Nanofiber interface macros
%========================================================================================
\newcommand{\HE}{HE$_{11}$\index{mode!HE11 mode@HE$_{11}$ mode}}

\newcommand{\expect}[1]{\big\langle #1 \big\rangle}
\newcommand{\expects}[1]{\langle #1 \rangle}
\newcommand{\melement}[3]{\langle #1 \lvert #2 \rvert #3 \rangle}
\newcommand{\Ip}[2]{\left\langle {#1},{#2} \right\rangle}
\newcommand{\modsq}[1]{\lvert #1 \rvert^2}
\newcommand{\normsq}[1]{\lVert #1 \rVert^2}
\newcommand{\grad}{\nabla}
\newcommand{\partialD}[2]{\frac{\partial #1}{\partial #2}}

\newcommand{\peakprobe}{\mathcal{E}_0}
\newcommand{\eff}{\text{eff}}
\newcommand{\varFz}{\big( \Delta F_z^{0} \big)^2}
\newcommand{\varFzText}{( \Delta F_z^{0} )^2}
\newcommand{\varFzLG}{( \Delta F_z^{00} )^2}

\newcommand{\rperp}{\mathbf{r}_\perp}
\newcommand{\talphan}{\overset{\leftrightarrow}{\alpha}{}^{(n)}}
\newcommand{\talphanOp}{\hat{\overset{\leftrightarrow}{\alpha}}{}^{(n)}}

%\newcommand{\Cstrength}{ \chi^{(1)}\sqrt{ \dot{N}_L } }
%\newcommand{\CstrengthSq}{ \big(\chi^{(1)} \big)^2 \dot{N}_L }
\newcommand{\Cstrength}{ \sqrt{\kappa} }
\newcommand{\CstrengthSq}{ \kappa }

%\newcommand{\srf}[1]{Sec. \ref{#1}}
\newcommand{\half}{\smallfrac{1}{2}}
%========================================================================
% Square waveguide interface macros
%========================================================================
\newcommand{\SWG}{square waveguide\xspace} %{SWG\xspace}%
\newcommand{\TE}{TE$_{01}$\index{mode!TE01 mode@TE$_{01}$ mode}}
\newcommand{\TM}{TM$_{01}$\index{mode!TM01 mode@TM$_{01}$ mode}}

%==== subscripts ======
\newcommand{\oneD}{{\rm 1D}}
\newcommand{\vac}{{\rm vac}}
\newcommand{\cav}{{\rm cav}}
\newcommand{\inp}{{\rm in}}
\newcommand{\out}{{\rm out}}
\newcommand{\inter}{{\rm int}}
\newcommand{\scs}{{\rm SCS}}
\newcommand{\fwd}{+}
\newcommand{\bwd}{-}
\newcommand{\trans}{+}
\newcommand{\refl}{-}

 %==== operators/moments ======
\newcommand{\der}[1]{\frac{d {#1}}{dt}}
\newcommand{\unittens}{\tensor{\mathbf{I}}}
\newcommand{\poltens}{\hat{\tensor{\boldsymbol{\alpha}}}}
\newcommand{\varz}{\Delta J_z^2}
\newcommand{\jx}{\hat{J}_x}
\newcommand{\jz}{\hat{J}_z}
\newcommand{\shotnoise}{\Delta \mathcal{M}^2 |_{\rm SN}}
\newcommand{\projnoise}{\Delta \mathcal{M}^2_{\rm PN}}
\newcommand{\polcomp}{\hat{K}} % p,p' component of the tensor polarizability
\newcommand{\fo}{\hat{\mathbf{f}}}
\newcommand{\fx}{\hat{f}_x}
\newcommand{\fy}{\hat{f}_y}
\newcommand{\fz}{\hat{f}_z}
\newcommand{\Fx}{\hat{F}_x}
\newcommand{\Fy}{\hat{F}_y}
\newcommand{\Fz}{\hat{F}_z}
\newcommand{\rhoo}{\hat{\rho}}

%==== Microscopic moments for the qubit/qutrit subspace ====
\newcommand{\sigmauu}{\hat{\sigma}_{\uparrow\uparrow}}
\newcommand{\sigmaud}{\hat{\sigma}_{\uparrow\downarrow}}
\newcommand{\sigmaut}{\hat{\sigma}_{\uparrow \mathrm{T}}}
\newcommand{\sigmadu}{\hat{\sigma}_{\downarrow\uparrow}}
\newcommand{\sigmadd}{\hat{\sigma}_{\downarrow\downarrow}}
\newcommand{\sigmadt}{\hat{\sigma}_{\downarrow \mathrm{T}}}
\newcommand{\sigmatu}{\hat{\sigma}_{\mathrm{T}\uparrow}}
\newcommand{\sigmatd}{\hat{\sigma}_{\mathrm{T}\downarrow}}
\newcommand{\sigmatt}{\hat{\sigma}_{\mathrm{T}\mathrm{T}}}
\newcommand{\sigmaab}{\hat{\sigma}_{ab}}
\newcommand{\sigmaba}{\hat{\sigma}_{ba}}
\newcommand{\sigmadc}{\hat{\sigma}_{dc}}
\newcommand{\sigmacd}{\hat{\sigma}_{cd}}

\newcommand{\Dsigmauu}{\Delta\sigma_{\uparrow\uparrow}}
\newcommand{\Dsigmaud}{\Delta\sigma_{\uparrow\downarrow}}
\newcommand{\Dsigmaut}{\Delta\sigma_{\uparrow \mathrm{T}}}
\newcommand{\Dsigmadu}{\Delta\sigma_{\downarrow\uparrow}}
\newcommand{\Dsigmadd}{\Delta\sigma_{\downarrow\downarrow}}
\newcommand{\Dsigmadt}{\Delta\sigma_{\downarrow \mathrm{T}}}
\newcommand{\Dsigmatu}{\Delta\sigma_{\mathrm{T}\uparrow}}
\newcommand{\Dsigmatd}{\Delta\sigma_{\mathrm{T}\downarrow}}
\newcommand{\Dsigmatt}{\Delta\sigma_{\mathrm{T}\mathrm{T}}}
\newcommand{\Dsigmaab}{\Delta\sigma_{ab}}
\newcommand{\Dsigmaba}{\Delta\sigma_{ba}}
\newcommand{\Dsigmadc}{\Delta\sigma_{dc}}
\newcommand{\Dsigmacd}{\Delta\sigma_{cd}}

%==== physical parameters ======
\newcommand{\Eamp}{\mathcal{F}_0^{(+)}}
\newcommand{\charpol}{\alpha_0(\Delta_{f\!f'})}
\newcommand{\charpolq}{\alpha_0(\Delta_{f\!f'}^q)}
\newcommand{\qaxis}{\mathbf{e}_{\tilde{z}}}
\newcommand{\qangle}{\varphi}
\newcommand{\magic}[1]{\tilde{\omega}_{#1}}
\newcommand{\chiN}{\chi_{N}}
\newcommand{\NA}{N_C}
\newcommand{\chieff}{\chi_{\raisebox{-.1pt}{\tiny $J_3$}}}

%==== scattering and optical pumping rates ====%
\newcommand{\gammauu}{\gamma_{\uparrow \rightarrow \uparrow}}
\newcommand{\gammadd}{\gamma_{\downarrow \rightarrow \downarrow}}
\newcommand{\gammaud}{\gamma_{\uparrow \rightarrow \downarrow}}
\newcommand{\gammadu}{\gamma_{\downarrow \rightarrow \uparrow}}
\newcommand{\gammau}{\gamma_{\uparrow}}
\newcommand{\gammad}{\gamma_{\downarrow}}

%==== effective areas ======
\newcommand{\Ain}{A_{\rm in}}
\newcommand{\Abir}{A_N}
\newcommand{\AF}{A_F} % for the Faraday protocol.
\newcommand{\Ai}{A_0} % for the input light.
\newcommand{\Aint}{A_{\rm int}} % for the interaction area.

%==== eigenfunctions ======
\newcommand{\eigenf}{\mbf{f}_\eta}
\newcommand{\eigenfp}{\mbf{f}_{\eta'}}
\newcommand{\eigeng}{\mbf{g}_\eta}
\newcommand{\eigengp}{\mbf{g}_{\eta'}}

%==== field operators ======
\newcommand{\awg}{\hat{a}_{b,p}(\omega)}
\newcommand{\awr}{\hat{a}_{m,p}(\omega,\beta)}
%========================================================================================
%  Spacing macros
%========================================================================================
%\newcommand{\ssp}{\hspace{0.4pt}}%small horizontal space
\newcommand{\nsp}{\hspace{-0.7pt}}%negative horizontal space
%========================================================================================




% For faster processing, load Matlab syntax for listings
\lstloadlanguages{Matlab}% use listings package
\lstset{language=Matlab,
        frame=single,
        basicstyle=\small\ttfamily,
        keywordstyle=[1]\color{Blue}\bf,
        keywordstyle=[2]\color{Purple},
        keywordstyle=[3]\color{Blue}\underbar,
        identifierstyle=,
        commentstyle=\usefont{T1}{pcr}{m}{sl}\color{MyDarkGreen}\small,
        stringstyle=\color{Purple},
        showstringspaces=false,
        tabsize=5,
        % Put standard MATLAB functions not included in the default
        % language here
        morekeywords={xlim,ylim,var,alpha,factorial,poissrnd,normpdf,normcdf},
        % Put MATLAB function parameters here
        morekeywords=[2]{on, off, interp},
        % Put user defined functions here
        morekeywords=[3]{FindESS},
        morecomment=[l][\color{Blue}]{...},
        numbers=left,
        firstnumber=1,
        numberstyle=\tiny\color{Blue},
        stepnumber=5
        }
        
        
        
% Includes a figure
% The first parameter is the label, which is also the name of the figure
%   with or without the extension (e.g., .eps, .fig, .png, .gif, etc.)
%   IF NO EXTENSION IS GIVEN, LaTeX will look for the most appropriate one.
%   This means that if a DVI (or PS) is being produced, it will look for
%   an eps. If a PDF is being produced, it will look for nearly anything
%   else (gif, jpg, png, et cetera). Because of this, when I generate figures
%   I typically generate an eps and a png to allow me the most flexibility
%   when rendering my document.
% The second parameter is the width of the figure normalized to column width
%   (e.g. 0.5 for half a column, 0.75 for 75% of the column)
% The third parameter is the caption.
\newcommand{\scalefig}[3]{
  \begin{figure}[ht!]
    % Requires \usepackage{graphicx}
    \centering
    \includegraphics[width=#2\columnwidth]{#1}
    %%% I think \captionwidth (see above) can go away as long as
    %%% \centering is above
    %\captionwidth{#2\columnwidth}%
    \caption{#3}
    \label{#1}
  \end{figure}}
  
% Similar to \scalefig, the following allows a shortened caption and a width not fixing the unit. The figure will be aligned with the float-friendly [!tbp] option.
\newcommand{\includefig}[5]{
  \begin{figure}[#5]
    % Requires \usepackage{graphicx}
    \centering
    \includegraphics[width=#2]{#1}
    \caption[#4]{#3}
    \label{#1}
  \end{figure}}
  
%\DeclareMathVersion{normal2}